package com.uottawa.clickerapp.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.json.GsonHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import com.uottawa.clickerapp.domain.Answer;
import com.uottawa.clickerapp.domain.QuestionSession;
import com.uottawa.clickerapp.service.ClickerAppWebServiceClient;

public class ClickerAppWebServiceClientImpl implements ClickerAppWebServiceClient {
	
	private RestTemplate restTemplate = new RestTemplate();
	// replace the host ID with your router ID or the public domain
	private String baseUrl = "http://137.122.91.175:8080/ClickerApp/json";
	private String sessionId;
	private static ClickerAppWebServiceClient singleton;
	
	private ClickerAppWebServiceClientImpl() {
		// do nothing.
	}
	
	public static ClickerAppWebServiceClient getInstance() {
		if(singleton == null) {
			singleton = new ClickerAppWebServiceClientImpl();
			singleton.getRestTemplate().getMessageConverters().add(new GsonHttpMessageConverter());
			return singleton;
		} else {
			return singleton; 
		}
	}
	
	public RestTemplate getRestTemplate() {
		return restTemplate;
	}

	public String submitPoll2(Long questionID, Long answerID) {
		String url = baseUrl + "/submit/{questionID}/{answerID}";
		Map<String, Long> vars = new HashMap<String, Long>();
		vars.put("questionID", questionID);
		vars.put("answerID", answerID);
		String result = restTemplate.getForObject(url, String.class, vars);
		return result;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public String submitPoll(Long questionID, Long answerID) {
		String url = baseUrl + "/submit/{questionID}/{answerID}";
		HttpHeaders requestHeaders = new HttpHeaders();
		requestHeaders.add("Cookie", "JSESSIONID=" + this.sessionId);
		HttpEntity requestEntity = new HttpEntity(null, requestHeaders);
		ResponseEntity restResponse;
		try {
			restResponse = restTemplate.exchange(
				url,
			    HttpMethod.GET,
			    requestEntity,
			    String.class,
			    questionID, answerID);
			String result = restResponse.getBody().toString();
			return result;
		} catch(HttpMessageNotReadableException e){}
		
		return null;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public QuestionSession getQuestions(String keyword) {
		String url = baseUrl + "/getquestions/{keyword}";
		HttpHeaders requestHeaders = new HttpHeaders();
		requestHeaders.add("Cookie", "JSESSIONID=" + this.sessionId);
		HttpEntity requestEntity = new HttpEntity(null, requestHeaders);
		ResponseEntity restResponse = restTemplate.exchange(
			url,
		    HttpMethod.GET,
		    requestEntity,
		    QuestionSession.class,
		    keyword);
		QuestionSession result = (QuestionSession) restResponse.getBody();
		return result;
	}

	@SuppressWarnings("unchecked")
	public List<Long> getResults(Long questionID) {
		String url = baseUrl + "/getResults/{questionID}";
		List<Long> result = restTemplate.getForObject(url, List.class, questionID);
		return result;
	}
	
	@SuppressWarnings("unchecked")
	public List<Answer> getAnswers(Long questionID) {
		String url = baseUrl + "/getAnswers/{questionID}";
		List<Answer> result = restTemplate.getForObject(url, List.class, questionID);
		return result;
	}
	
	@SuppressWarnings("unchecked")
	public List<QuestionSession> searchSessions(String keyword) {
		String url = baseUrl + "/searchSessions/{keyword}";
		List<QuestionSession> result = restTemplate.getForObject(url, List.class, keyword);
		return result;
	}

	@SuppressWarnings("unchecked")
	public List<String> getRecentSessions() {
		String sessionIdUrl = baseUrl + "/newsessioID";
		String sessionId = restTemplate.getForObject(sessionIdUrl, String.class);	
		this.sessionId = sessionId;
		
		String url = baseUrl + "/recentsessions";
		List<String> result = restTemplate.getForObject(url, List.class);

		return result;
	}

}
